/**
 * 
 */
package com.bskms.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.bskms.utils.AIRecipeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.bskms.bean.Food;
import com.bskms.bean.Material;
import com.bskms.mapper.FoodMapper;
import com.bskms.mapper.MaterialMapper;
import com.bskms.model.MMGridPageVoBean;
import com.bskms.service.FoodService;

@Service
public class FoodServiceImpl implements FoodService{
	@Autowired
	private FoodMapper foodMapper;
	
	private final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

	@Override
	public Object getAllFoodByLimit(Food food) {
		int size = 0;

		Integer begin = (food.getPage() - 1) * food.getLimit();
		food.setPage(begin);

		List<Food> rows = new ArrayList<>();
		try {
			rows = foodMapper.getAllFoodByLimit(food);
			size = foodMapper.countAllFoodByLimit(food);
		} catch (Exception e) {
			logger.error("根据查询菜单 异常", e);
		}
		MMGridPageVoBean<Food> vo = new MMGridPageVoBean<>();
		vo.setTotal(size);
		vo.setRows(rows);

		return vo;
	}

	@Override
	public Food selectByPrimaryKey(Integer id) {
		return foodMapper.selectByPrimaryKey(id);
	}

	@Override
	public void addFood(Food food) {
		try {
			foodMapper.insert(food);
			}catch (Exception e) {
				e.printStackTrace();
		}
	}

	@Override
	public String updateFood(Food food) {
		try {
			foodMapper.updateByPrimaryKeySelective(food);
			return "SUCCESS";
		} catch (Exception e) {
			logger.error("根据菜单id更新菜单异常", e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return "ERR";
		}
	}

	@Override
	public void delFoodById(Integer id) {
		try {
			foodMapper.deleteByPrimaryKey(id);
		} catch (Exception e) {
			logger.error("删除菜单信息出现异常", e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
	}

	// 新增：实现AI推荐方法（直接调用工具类）
	@Override
	public String getAIRecipeRecommend(String userDemand) {
		try {
			return AIRecipeUtils.getAIRecipeRecommend(userDemand);
		} catch (Exception e) {
			logger.error("AI菜谱推荐服务异常", e);
			return "AI推荐失败，请稍后重试";
		}
	}

}
